package com.job.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.job.dao.BaseDao.PageResult;
import com.job.dao.ResourceDao;
import com.job.model.admin.Resource;
import com.job.service.ResourceService;

@Component("resourceService")
public class ResourceServiceImpl implements ResourceService {
	private ResourceDao resourceDao;
	
	public ResourceDao getResourceDao() {
		return resourceDao;
	}
	@javax.annotation.Resource(name="resourceDao")
	public void setResourceDao(ResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}
	@Override
	public PageResult<Resource> findResourcePageResult(String queryName, int pageSize,
			int currentPageNo) {
		PageResult<Resource> pr = resourceDao.getPageResult(queryName, pageSize, currentPageNo);
		List<Resource> resources = pr.getResult();
		if(resources.size()>0){
//			Set<Resource> rootResource = new HashSet<Resource>();
//			for(Resource resource:resources){
//				achieveRootMenu(resource,rootResource);
//			}
			List<Resource> result = new ArrayList<Resource>();
			for(Resource root:resources){
				Resource menu = new Resource();
				menu.setId(root.getId());
				menu.setName(root.getName());
				menu.setPath(root.getPath());
				ergodicRootResource(root,menu);
				result.add(menu);
			}
			pr.setResult(result);
		}
		return pr;
	}
//	private void achieveRootMenu(Resource resource,Set<Resource> rootResource){
//		Resource parentResource = resource.getParentResource();
//		if(parentResource!=null){
//			achieveRootMenu(resource.getParentResource(),rootResource);
//		}else{
//			rootResource.add(resource);
//		}
//	}
//	private Resource achieveRootMenu(Resource resource,Resource childResource){
//		childResource.setId(resource.getId());
//		childResource.setName(resource.getName());
//		childResource.setPath(resource.getPath());
//		Resource parentResource = resource.getParentResource();
//		if(parentResource!=null){
//			Resource rootNode = new Resource();
//			rootNode.setId(parentResource.getId());
//			rootNode.setName(parentResource.getName());
//			rootNode.setPath(parentResource.getPath());
//			childResource.setParentResource(rootNode);
//			Set<Resource> ress = new HashSet<Resource>();
//			ress.add(childResource);
//			
//			rootNode.setResources(ress);
//			return achieveRootMenu(parentResource,rootNode);
//		}else{
//			return childResource;
//		}
//	}
	private void ergodicRootResource(Resource resource,Resource resultResource){
		Set<Resource> childResource = resource.getResources();
		if(childResource!=null){
			resultResource.setResources(new HashSet<Resource>());
		}
		for(Resource r:childResource){
			Resource menu = new Resource();
			menu.setId(r.getId());
			menu.setName(r.getName());
			menu.setPath(r.getPath());
			resultResource.getResources().add(menu);
			ergodicRootResource(r,menu);
		}
	}
	
	

}
